package com.mi.rule.db.succinct.test;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mi.rule.db.core.MiPage;
import com.mi.rule.db.dao.MiDao;
import com.mi.rule.db.succinct.entity.Demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 使用方式 二
 * 通过MiDao操作数据库
 * 增 删 改 查 统计 分页 多表联查
 */
@Slf4j
@RestController
@RequestMapping("/template2")
public class Template_2 {
    @Autowired
    private MiDao miDao;

    /**
     * 增
     */
    @RequestMapping("/insert")
    @Transactional(rollbackFor = Exception.class)
    public Map<Object, Object> insertOne(@RequestBody Map<Object, Object> map) {
        //初始化数据
        miDao.delete("delete from 0_demo where id != 0");

        //insert
        long insert = miDao.insert("insert into 0_demo (id,name) values (1,'小明') , (2,2) , (3,3)");

        //insert-map
        Map<String, Object> demo1 = new HashMap<>();
        demo1.put("name", "数据库自增，并返回ID");
        long insert1 = miDao.insert("0_demo", demo1);

        //insert-map
        Map<String, Object> demo2 = new HashMap<>();
        demo2.put("id", 6);
        demo2.put("name", "手动设置ID");
        long insert2 = miDao.insert("0_demo", demo2);


        //操作完成
        log.info("end");
        map.put("操作完成", "true");
        return map;
    }

    /**
     * 删
     */
    @RequestMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public Map<Object, Object> delete(@RequestBody Map<Object, Object> map) {
        //初始化数据
        initData();


        //delete
        long delete1 = miDao.delete("delete from 0_demo where id = 10");


        //deleteById
        long l1 = miDao.deleteById("0_demo", 90);


        //deleteByIds
        long l2 = miDao.deleteByIds("0_demo", Arrays.asList(8, 9, 10));


        map.put("操作完成", "true");
        return map;
    }

    /**
     * 改
     */
    @RequestMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public Map<Object, Object> update(@RequestBody Map<Object, Object> map) {
        //初始化数据
        initData();


        //update
        long update = miDao.update("update 0_demo set name='略略略...' where id =33");


        //updateById
        Map<Object, Object> updateMap = new HashMap<>();
        updateMap.put("name", "updateById-10");
        long l1 = miDao.updateById("0_demo", updateMap, 10);


        //updateByIds
        Map<Object, Object> updateMap2 = new HashMap<>();
        updateMap2.put("name", "updateByIds-55-56-57");
        long l2 = miDao.updateByIds("0_demo", updateMap2, Arrays.asList(55, 56, 57));


        //TODO--updateBatch
        //        List<Map<Object, Object>> listUpdate5 = new ArrayList<>();
        //        for (int i = 80; i < 100; i++) {
        //            Map<Object, Object> mapNew5 = new HashMap<>();
        //            mapNew5.put("id", i + "");
        //            mapNew5.put("name", "我是批量修改 " + i + "");
        //            mapNew5.put("create_time", LocalDateTime.now().plusDays(10));
        //            listUpdate5.add(mapNew5);
        //        }

        map = new HashMap<>();
        map.put("操作完成", "true");
        return map;
    }

    /**
     * 查
     */
    @RequestMapping("/select")
    @Transactional(rollbackFor = Exception.class)
    public Map<Object, Object> select(@RequestBody Map<Object, Object> map) {
        //初始化数据
        initData();

        //selectOne
        miDao.selectOne("select * from 0_demo");

        //list
        miDao.list("select * from 0_demo");

        //listObjs
        miDao.listObjs("select id from 0_demo");


        map.put("操作完成", "true");
        return map;
    }

    /**
     * 统计
     */
    @RequestMapping("/count")
    @Transactional(rollbackFor = Exception.class)
    public Map<Object, Object> count(@RequestBody Map<Object, Object> map) {
        //初始化数据
        initData();

        //count
        long count = miDao.count("select count(1) from 0_demo where id > 10");

        map = new HashMap<>();
        map.put("操作完成", "true");
        return map;
    }

    /**
     * 分页
     */
    @RequestMapping("/page")
    @Transactional(rollbackFor = Exception.class)
    public Map<Object, Object> page(@RequestBody Map<Object, Object> map) {
        //初始化数据
        initData();


        //查询所有字段
        miDao.page(new MiPage<>(1, 10), "select * from 0_demo where id != 1");


        map = new HashMap<>();
        map.put("操作完成", "true");
        return map;
    }

    /**
     * 多表联查
     * 不用构造器也可以纯手写字符串-道理是一样的
     */
    @RequestMapping("/join")
    @Transactional(rollbackFor = Exception.class)
    public Map<Object, Object> join(@RequestBody Map<Object, Object> map) {
        //初始化数据
        initData();

        //  主表0_demo 联查三张表
        //
        //1.联查0_file表name字段 as fileName
        //2.联查0_user表name字段 as userName
        //3.联查0_type表name字段 as typeName
        String wrapper_left_sql_string =
                "select de.* ,file.name as fileName ,user.name as userName ,type.name as typeName" +
                        " from 0_demo de " +
                        " left join 0_file file on file.id = de.file_id " +
                        " left join 0_user user on user.id = de.user_id " +
                        " left join 0_type type on type.id = de.type_id " +
                        " where  de.id in (1,2,3,4,5,6,7,8)  and de.id !=999 and de.name !='999'";

        //查询结果
        Map<String, Object> stringObjectMap = miDao.selectOne(wrapper_left_sql_string);
        List<Map<String, Object>> list = miDao.list(wrapper_left_sql_string);
        List<Object> objects = miDao.listObjs(wrapper_left_sql_string);
        IPage<Map<String, Object>> page = miDao.page(new MiPage<>(1, 10), wrapper_left_sql_string);


        map = new HashMap<>();
        map.put("操作完成", "true");
        return map;
    }

    private void initData() {
        log.info("start");
        long delete = miDao.delete("delete from 0_demo where id != 0");
        List<Demo> arrayList1 = new ArrayList<>();
        for (int i = 1; i < 300; i++) {
            Demo demo = new Demo();
            demo.setId(i + "");
            demo.setName(i + "");
            demo.setCreateTime(LocalDateTime.now());
            arrayList1.add(demo);
        }
        long l = miDao.insertBatch(arrayList1, 100);
    }

}
